 package handling.channel.handler;

 import client.MapleCharacter;
 import client.MapleClient;
 import client.MapleDisease;
 import client.inventory.Item;
 import client.inventory.MapleInventoryType;
 import client.inventory.MaplePet;
 import client.inventory.PetCommand;
 import client.inventory.PetDataFactory;
 import client.inventory.PetFlag;
 import client.skills.Skill;
 import client.skills.SkillFactory;
 import configs.ServerConfig;
 import constants.GameConstants;
 import java.awt.Point;
 import java.util.List;
 import server.MapleInventoryManipulator;
 import server.MapleItemInformationProvider;
 import server.maps.FieldLimitType;
 import server.movement.LifeMovementFragment;
 import server.quest.MapleQuest;
 import tools.MaplePacketCreator;
 import tools.Randomizer;
 import tools.data.input.LittleEndianAccessor;
 import tools.packet.EffectPacket;
 import tools.packet.PetPacket;

 public class PetHandler
 {
   public static void SpawnPet(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/*  32 */     chr.updateTick(slea.readInt());
/*  33 */     chr.spawnPet((short)slea.readByteAsInt(), (slea.readByte() > 0));
   }




   public static void Pet_AutoBuff(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/*  40 */     int petid = slea.readInt();
/*  41 */     MaplePet pet = chr.getSpawnPet(petid);
/*  42 */     if (chr.getMap() == null || pet == null) {
       return;
     }
/*  45 */     int skillId = slea.readInt();
/*  46 */     Skill buffId = SkillFactory.getSkill(skillId);
/*  47 */     if (chr.getSkillLevel(buffId) > 0 || skillId == 0) {
/*  48 */       pet.setBuffSkill(skillId);
/*  49 */       chr.petUpdateStats(pet, true);
     }
/*  51 */     c.announce(MaplePacketCreator.enableActions());
   }




   public static void Pet_AutoPotion(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/*  58 */     slea.skip(1);
/*  59 */     if (chr == null) {
/*  60 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  63 */     chr.updateTick(slea.readInt());
/*  64 */     short slot = slea.readShort();
/*  65 */     if (chr == null || !chr.isAlive() || chr.getMapId() == 749040100 || chr.getMap() == null || chr.hasDisease(MapleDisease.无法使用药水)) {
/*  66 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  69 */     Item toUse = chr.getInventory(MapleInventoryType.USE).getItem(slot);
/*  70 */     if (toUse == null || toUse.getQuantity() < 1 || toUse.getItemId() != slea.readInt()) {
/*  71 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  74 */     long time = System.currentTimeMillis();
/*  75 */     if (chr.getNextConsume() > time) {
/*  76 */       chr.dropMessage(5, "暂时无法使用道具.");
/*  77 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  80 */     if (!FieldLimitType.PotionUse.check(chr.getMap().getFieldLimit())) {
/*  81 */       if (MapleItemInformationProvider.getInstance().getItemEffect(toUse.getItemId()).applyTo(chr)) {
/*  82 */         MapleInventoryManipulator.removeFromSlot(c, MapleInventoryType.USE, slot, (short)1, false);
/*  83 */         if (chr.getMap().getConsumeItemCoolTime() > 0) {
/*  84 */           chr.setNextConsume(time + (chr.getMap().getConsumeItemCoolTime() * 1000));
         }
       }
     } else {
/*  88 */       c.announce(MaplePacketCreator.enableActions());
     }
   }







   public static void PetExcludeItems(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/*  99 */     int petSlot = slea.readInt();
/* 100 */     MaplePet pet = chr.getSpawnPet(petSlot);
/* 101 */     if (pet == null || !PetFlag.PET_IGNORE_PICKUP.check(pet.getFlags())) {
/* 102 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 105 */     pet.clearExcluded();
/* 106 */     byte amount = slea.readByte();
/* 107 */     for (int i = 0; i < amount; i++) {
/* 108 */       pet.addExcluded(i, slea.readInt());
     }
   }











   public static void PetChat(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/* 123 */     if (slea.available() < 12L) {
/* 124 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 127 */     int petid = slea.readInt();
/* 128 */     slea.readInt();
/* 129 */     if (chr == null || chr.getMap() == null || chr.getSpawnPet(petid) == null) {
       return;
     }
/* 132 */     short act = slea.readShort();
/* 133 */     String text = slea.readMapleAsciiString();
/* 134 */     if (text.length() < 1) {
       return;
     }

/* 138 */     chr.getMap().broadcastMessage(chr, PetPacket.petChat(chr.getId(), act, text, (short)(byte)petid), true);
   }










   public static void PetCommand(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/* 151 */     int petId = slea.readInt();
/* 152 */     MaplePet pet = null;
/* 153 */     pet = chr.getSpawnPet((byte)petId);
/* 154 */     slea.readByte();
/* 155 */     if (pet == null) {
/* 156 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 159 */     byte command = slea.readByte();
/* 160 */     PetCommand petCommand = PetDataFactory.getPetCommand(pet.getPetItemId(), command);
/* 161 */     if (petCommand == null) {
/* 162 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 165 */     byte petIndex = chr.getPetIndex(pet);
/* 166 */     boolean success = false;
/* 167 */     if (Randomizer.nextInt(99) <= petCommand.getProbability()) {
/* 168 */       success = true;
/* 169 */       if (pet.getCloseness() < 30000) {
/* 170 */         int newCloseness = pet.getCloseness() + petCommand.getIncrease() * ServerConfig.CHANNEL_RATE_TRAIT;
/* 171 */         if (newCloseness > 30000) {
/* 172 */           newCloseness = 30000;
         }
/* 174 */         pet.setCloseness(newCloseness);
/* 175 */         if (newCloseness >= GameConstants.getClosenessNeededForLevel(pet.getLevel() + 1)) {
/* 176 */           pet.setLevel(pet.getLevel() + 1);
/* 177 */           c.announce(EffectPacket.showOwnPetLevelUp(petIndex));
/* 178 */           chr.getMap().broadcastMessage(EffectPacket.showPetLevelUp(chr.getId(), petIndex));
         }
/* 180 */         chr.petUpdateStats(pet, true);
       }
     }
/* 183 */     chr.getMap().broadcastMessage(PetPacket.commandResponse(chr.getId(), (byte)petCommand.getCommand(), (short)petIndex, success, false));
   }










   public static void PetFood(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/* 196 */     if (chr == null || chr.getMap() == null) {
       return;
     }
/* 199 */     int previousFullness = 100;
/* 200 */     byte petslot = 0;
/* 201 */     MaplePet[] pets = chr.getSpawnPets(); byte i;
/* 202 */     for (i = 0; i < 3; i = (byte)(i + 1)) {
/* 203 */       if (pets[i] != null && pets[i].getFullness() < previousFullness) {
/* 204 */         petslot = i;
         break;
       }
     }
/* 208 */     MaplePet pet = chr.getSpawnPet(petslot);
/* 209 */     chr.updateTick(slea.readInt());
/* 210 */     short slot = slea.readShort();
/* 211 */     int itemId = slea.readInt();
/* 212 */     Item petFood = chr.getInventory(MapleInventoryType.USE).getItem(slot);
/* 213 */     if (pet == null || petFood == null || petFood.getItemId() != itemId || petFood.getQuantity() <= 0 || itemId / 10000 != 212) {
/* 214 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 217 */     boolean gainCloseness = false;
/* 218 */     if (Randomizer.nextInt(101) > 50) {
/* 219 */       gainCloseness = true;
     }
/* 221 */     if (pet.getFullness() < 100) {
/* 222 */       int newFullness = pet.getFullness() + 30;
/* 223 */       if (newFullness > 100) {
/* 224 */         newFullness = 100;
       }
/* 226 */       pet.setFullness(newFullness);
/* 227 */       byte index = chr.getPetIndex(pet);
/* 228 */       if (gainCloseness && pet.getCloseness() < 30000) {
/* 229 */         int newCloseness = pet.getCloseness() + 1;
/* 230 */         if (newCloseness > 30000) {
/* 231 */           newCloseness = 30000;
         }
/* 233 */         pet.setCloseness(newCloseness);
/* 234 */         if (newCloseness >= GameConstants.getClosenessNeededForLevel(pet.getLevel() + 1)) {
/* 235 */           pet.setLevel(pet.getLevel() + 1);
/* 236 */           c.announce(EffectPacket.showOwnPetLevelUp(index));
/* 237 */           chr.getMap().broadcastMessage(EffectPacket.showPetLevelUp(chr.getId(), index));
         }
       }
/* 240 */       chr.petUpdateStats(pet, true);
/* 241 */       chr.getMap().broadcastMessage(c.getPlayer(), PetPacket.commandResponse(chr.getId(), (byte)1, (short)index, true, true), true);
     } else {
/* 243 */       if (gainCloseness) {
/* 244 */         int newCloseness = pet.getCloseness() - 1;
/* 245 */         if (newCloseness < 0) {
/* 246 */           newCloseness = 0;
         }
/* 248 */         pet.setCloseness(newCloseness);
/* 249 */         if (newCloseness < GameConstants.getClosenessNeededForLevel(pet.getLevel())) {
/* 250 */           pet.setLevel(pet.getLevel() - 1);
         }
/* 252 */         chr.dropMessage(5, "您的宠物的饥饿感是满值，如果继续使用将会有50%的几率减少1点亲密度。");
       }
/* 254 */       chr.petUpdateStats(pet, true);
/* 255 */       chr.getMap().broadcastMessage(chr, PetPacket.commandResponse(chr.getId(), (byte)1, (short)chr.getPetIndex(pet), false, true), true);
     }
/* 257 */     MapleInventoryManipulator.removeFromSlot(c, MapleInventoryType.USE, slot, (short)1, true, false);
/* 258 */     c.announce(MaplePacketCreator.enableActions());
   }




   public static void MovePet(LittleEndianAccessor slea, MapleCharacter chr) {
/* 265 */     int petSlot = slea.readInt();
/* 266 */     slea.skip(1);
/* 267 */     int v169 = slea.readInt();
/* 268 */     int tEncodedGatherDuration = slea.readInt();
/* 269 */     Point startPos = slea.readPos();
/* 270 */     Point velPos = slea.readPos();
/* 271 */     List<LifeMovementFragment> res = MovementParse.parseMovement(slea, 3);
/* 272 */     if (res != null && chr != null && !res.isEmpty() && chr.getMap() != null) {
/* 273 */       MaplePet pet = chr.getSpawnPet(petSlot);
/* 274 */       if (pet == null) {
         return;
       }
/* 277 */       startPos = pet.getPos();
/* 278 */       chr.getSpawnPet(chr.getPetIndex(pet)).updatePosition(res);
/* 279 */       chr.getMap().broadcastMessage(chr, PetPacket.movePet(chr.getId(), petSlot, v169, tEncodedGatherDuration, startPos, velPos, res), false);
     }
   }

   public static void AllowPetLoot(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/* 284 */     if (chr == null || chr.getMap() == null) {
/* 285 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 288 */     slea.skip(4);
/* 289 */     int data = slea.readShort();
/* 290 */     if (data > 0) {
/* 291 */       chr.getQuestNAdd(MapleQuest.getInstance(122902)).setCustomData(String.valueOf(data));
     } else {
/* 293 */       chr.getQuestRemove(MapleQuest.getInstance(122902));
     }
/* 295 */     MaplePet[] pet = c.getPlayer().getSpawnPets();
/* 296 */     for (int i = 0; i < 3; i++) {
/* 297 */       if (pet[i] != null && pet[i].getSummoned()) {
/* 298 */         pet[i].setCanPickup((data > 0));
/* 299 */         chr.petUpdateStats(pet[i], true);
       }
     }
/* 302 */     c.announce(PetPacket.showPetPickUpMsg((data > 0), 1));
   }

   public static void AllowPetAutoEat(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/* 306 */     if (chr == null || chr.getMap() == null) {
/* 307 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/* 310 */     slea.skip(4);
/* 311 */     slea.skip(4);
/* 312 */     boolean data = (slea.readByte() > 0);
/* 313 */     chr.updateInfoQuest(12334, data ? "autoEat=1" : "autoEat=0");
/* 314 */     c.announce(PetPacket.showPetAutoEatMsg());
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\PetHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */